package com.ruoyi.wwxkf.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.wwxkf.service.WwxKfNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 企业微信时间回调函数
 */
@Anonymous
@RestController
@RequestMapping("/wwxKf")
public class WwxKfNotifyController {
    @Autowired
    private WwxKfNotifyService wwxKfNotifyService;

    /**
     * 处理回调信息和事件
     *
     * @param xmlData
     * @param msgSignature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @param requestMethod
     * @return
     */
    @Anonymous
    @RequestMapping(value = "/notify", method = {RequestMethod.GET, RequestMethod.POST})
    public String handleNotify(@RequestBody(required = false) String xmlData,
                               @RequestParam("msg_signature") String msgSignature,
                               @RequestParam("timestamp") String timestamp,
                               @RequestParam("nonce") String nonce,
                               @RequestParam("echostr") String echostr,
                               @RequestHeader("X-HTTP-Method-Override") String requestMethod) {
        if ("POST".equalsIgnoreCase(requestMethod)) {
            return wwxKfNotifyService.notifyMsg(msgSignature, timestamp, nonce, echostr, xmlData);
        } else {
            return wwxKfNotifyService.notifyMsg(msgSignature, timestamp, nonce, echostr, null);
        }
    }
}
